<template>
  <div class="login-container">
    <el-form
      class="login-form"
      label-width="100px"
      :rules="rules"
      ref="registerForm"
      :model="form"
    >
      <div class="title-container">
        <img src="/logo.png" alt="logo" />
      </div>
      <el-form-item label="邮箱" prop="email">
        <el-input v-model="form.email" placeholder="请输入邮箱"></el-input>
      </el-form-item>
      <el-form-item label="验证码" prop="captcha" class="captcha-container">
        <div class="captcha">
          <img :src="code.captcha" alt="" @click="resetCaptcha" />
        </div>
        <el-input v-model="form.captcha" placeholder="请输入验证码"></el-input>
      </el-form-item>
      <el-form-item label="昵称" prop="nickname">
        <el-input v-model="form.nickname" placeholder="请输入昵称"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="passwd">
        <el-input
          type="password"
          v-model="form.passwd"
          placeholder="请输入密码"
        ></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="repasswd">
        <el-input
          type="password"
          v-model="form.repasswd"
          placeholder="请再次输入密码"
        ></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click.native.prevent="handleRegister"
          >注册</el-button
        >
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import md5 from "md5";

export default {
  layout: "login",
  methods: {
    handleRegister() {
      this.$refs.registerForm.validate(async (valid) => {
        if (valid) {
          console.log("校验成功");
          let obj = {
            email: this.form.email,
            nickname: this.form.nickname,
            passwd: md5(this.form.passwd),
            repasswd: this.form.repasswd,
            captcha: this.form.captcha,
          };
          let ret = await this.$http.post("/user/register", obj);
          if (ret.code === 0) {
            this.$alert("注册成功", "成功", {
              confirmButtonText: "去登陆",
              callback: () => {
                this.$router.push("/login");
              },
            });
          } else {
            this.$message.error(ret.message);
          }
        } else {
          console.log("校验失败");
        }
      });
    },
    resetCaptcha() {
      this.code.captcha = "/api/captcha?_t" + new Date().getTime();
    },
  },
  data() {
    return {
      form: {
        email: "111@qq.com",
        nickname: "wcly",
        passwd: "a123456",
        repasswd: "a123456",
        captcha: "",
      },
      rules: {
        email: [
          { required: true, message: "请输入邮箱" },
          { type: "email", message: "请输入正确的邮箱格式" },
        ],
        captcha: [{ required: true, message: "请输入验证码" }],
        nickname: [{ required: true, message: "请输入昵称" }],
        passwd: [
          {
            required: true,
            pattern: /^[\w_-]{6,12}$/g,
            message: "请输入6~12位密码",
          },
        ],
        repasswd: [
          { required: true, message: "请再次输入密码" },
          {
            validator: (rule, value, callback) => {
              if (value !== this.form.passwd) {
                callback(new Error("两次密码不一致"));
              }
              callback();
            },
          },
        ],
      },
      code: {
        captcha: "/api/captcha",
      },
    };
  },
};
</script>


<style lang="stylus"></style>